<?php
namespace Container;

class Player
{
    private $players        = NULL;
    private $mapSocketId    = NULL;
    private $mapIdSocket    = NULL;

    public function __construct()
    {
        $this->players      = array();
        $this->mapSocketId  = array();
        $this->mapIdSocket  = array();
    }

    public function getNextId()
    {
        for ($i = 1; $i <= \WebSocket::MAX_COUNT+1; ++$i)
        {
            if ( ! isset($this->players[$i])) return $i;
        }
        return NULL;
    }

    public function addPlayer($ip, $socket)
    {
        $id = $this->getNextId();
        $this->players[$id] = new \Entity\Player($id, $ip, $socket);

        $this->mapSocketId[$socket] = $id;
        $this->mapIdSocket[$id]     = $socket;
    }

    public function delPlayer($socket)
    {
        $id = $this->getIdBySocket($socket);
        unset($this->players[$id]);
        unset($this->mapSocketId[$socket]);
        unset($this->mapIdSocket[$id]);
        return $id;
    }

    public function getIdBySocket($socket)
    {
        return $this->mapSocketId[$socket];
    }
    
    public function getSocketById($id)
    {
        return $this->mapIdSocket[$id];
    }

    public function getPlayer($id)
    {
        return $this->players[$id];
    }
    
    public function issetPlayer($id)
    {
        return isset($this->players[$id]);
    }
    
    public function countPlayers()
    {
        return count($this->players);
    }
    
    public function getPlayers()
    {
        return $this->players;
    }
}

#eof
